package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.support.v7.app.AppCompatActivity
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RegExpValidatorUtils
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.CitySelectorActivityContract
import com.ewzj.bigdata.mvp.contract.PhoneBindingActivityContract
import com.ewzj.bigdata.mvp.model.bean.CitygroupBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqCityBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqPhoneBinding
import com.ewzj.bigdata.mvp.model.reqbean.ReqSearchCityBean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.CityBean
import com.ewzj.bigdata.mvp.model.rspbean.CitySearchBean
import com.ewzj.bigdata.mvp.ui.adapter.CityAdapter
import com.ewzj.bigdata.mvp.ui.adapter.HotcityAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import com.jess.arms.utils.UiUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.observables.GroupedObservable
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class PhoneBindingActivityPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: PhoneBindingActivityContract.View, model: PhoneBindingActivityContract.Model) : BasePresenter<PhoneBindingActivityContract.Model, PhoneBindingActivityContract.View>(model, view) {
    val context=mRootView as AppCompatActivity
    lateinit var dis: Disposable
    fun bindPhone(phone:String,code:String){
        if (phone.isNullOrEmpty()) {
            UiUtils.SnackbarText(context.getString(R.string.phoneempty))
            return
        }
        if (!RegExpValidatorUtils.IsHandset(phone)) {
            UiUtils.SnackbarText(context.getString(R.string.phoneillegal))
            return
        }
        if (code.isNullOrEmpty()) {
            UiUtils.SnackbarText(context.getString(R.string.codeillegal))
            return
        }
        mModel.phonebinding(ReqPhoneBinding(phone,code)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<BaseData<Any>>(mErrorHandler){
                    override fun onNext(t: BaseData<Any>?) {
                        UiUtils.SnackbarText("绑定成功")
                        context.finish()
                    }

                })
    }

    fun sendSms(phone: String) {
        if (phone.isNullOrEmpty()) {
            UiUtils.SnackbarText(context.getString(R.string.phoneempty))
            return
        }
        if (!RegExpValidatorUtils.IsHandset(phone)) {
            UiUtils.SnackbarText(context.getString(R.string.phoneillegal))
            return
        }
        mModel.sendmsg(phone).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<BaseData<Any>>(mErrorHandler){
                    override fun onNext(t: BaseData<Any>?) {
                        UiUtils.SnackbarText(context.resources.getString(R.string.smssuccess))
                    }
                })
        dis = Observable.interval(1, TimeUnit.SECONDS).compose(RxUtils.bindToLifecycle(mRootView))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Consumer<Long> {
                    override fun accept(along: Long) {
                        if (along < 60) {
                            mRootView.showEnableCode(along)
                        } else {
                            if (dis != null) {
                                dis.dispose()
                            }
                            mRootView.showAbleCode()
                        }
                    }
                })
    }
}